package com.parenting.controller;


import com.parenting.request.*;
import com.parenting.request.wechat.ForgetPasswordRequest;
import com.parenting.service.SystemUserService;
import com.parenting.util.TXSendSms;
import com.parenting.util.base.Result;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表信息 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-11
 */
@RestController
@Api(tags = "注册", description = "注册、发送验证码")
@RequestMapping("/user")
@Slf4j
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 新增 用户
     * @param user
     */
    @ApiOperation("注册")
    @PostMapping("register")
    public  Result registerUser(@RequestBody AppletRegisterUser user){

       return  systemUserService.registerUser(user);
    }

    @ApiOperation("发送验注册证码")
    @PostMapping("register/code/{phone}")
    public  Result registerUser(@PathVariable("phone") String phone){
        String[] phones = {"+86 "+phone};
        String newCode=TXSendSms.getNewcode();
        stringRedisTemplate.opsForValue().set("user:phone:"+phone,newCode,60, TimeUnit.SECONDS);
        String[] code = {newCode};
        try {
            TXSendSms.sendSms("AKIDOMMbJaiLFwiwueDVcU1kALI0Z9zbjzSn", "LITvFKBoCL0ed9P4dEuQkgOxAAlT5RaV",
                    "1400488934", "沃养商户", "879807", phones, code);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return  Result.buildSuccess();
    }


    @ApiOperation("发送验忘记密码的证码")
    @PostMapping("forget/code/{phone}")
    public  Result forgetUser(@PathVariable("phone") String phone){
        String[] phones = {"+86 "+phone};
        String newCode=TXSendSms.getNewcode();
        stringRedisTemplate.opsForValue().set("user:forget:"+phone,newCode,60, TimeUnit.SECONDS);
        String[] code = {newCode};
        try {
            TXSendSms.sendSms("AKIDOMMbJaiLFwiwueDVcU1kALI0Z9zbjzSn", "LITvFKBoCL0ed9P4dEuQkgOxAAlT5RaV",
                    "1400488934", "沃养商户", "879807", phones, code);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return  Result.buildSuccess();
    }

    @ApiOperation("忘记密码")
    @PostMapping("forget/password")
    public  Result forgetPassword(@RequestBody ForgetPasswordRequest forgetPasswordRequest){
        //1 判断验证码是否正确
        String s = stringRedisTemplate.opsForValue().get("user:forget:" + forgetPasswordRequest.getPhone());
        if (forgetPasswordRequest.getCode().equals(s)){
            return systemUserService.forgetPassword(forgetPasswordRequest);
        }else {
            return  Result.buildError("手机验证码不正确！");
        }

    }

}

